<template>
  <div class="app-container">
    <el-button type="primary" @click="addData">新增</el-button>
    <!-- 列表 -->
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top: 10px;" v-loading="listLoading">
      <el-table-column align="center" prop="className" :show-overflow-tooltip="true" label="驱动" />
      <el-table-column align="center" prop="url" :show-overflow-tooltip="true" label="连接地址" />
      <el-table-column align="center" prop="userName" :show-overflow-tooltip="true" label="连接用户名" />
      <el-table-column align="center" prop="password" :show-overflow-tooltip="true" label="连接密码">
        <template>
          ***********
        </template>
      </el-table-column>
      <el-table-column align="center" prop="dataType" label="数据库类型">
        <template slot-scope="scope">
          {{ scope.row.dataType === 0 ? 'MySql' : 'Oracle'}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="isDefault" label="是否默认">
        <template slot-scope="scope">
          {{ scope.row.isDefault ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createTime | gmtCreateFilter }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="100"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="removeHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="testHandle(scope.row.id)">测试连接</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页插件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[20, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 弹框 -->
    <el-dialog :title="formData.id ? '编辑' : '新增'" :visible.sync="dialogFormVisible">
      <el-form :model="formData" ref="formData" :rules="rules">
        <el-form-item label="驱动" :label-width="formLabelWidth" prop="className">
          <el-input v-model="formData.className" placeholder="驱动" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="连接地址" :label-width="formLabelWidth" prop="url">
          <el-input v-model="formData.url" placeholder="连接地址" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="连接用户名" :label-width="formLabelWidth" prop="userName">
          <el-input v-model="formData.userName" placeholder="连接用户名" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="连接密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="formData.password" placeholder="连接密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据库类型" :label-width="formLabelWidth" prop="dataType">
          <el-select v-model="formData.dataType" placeholder="请选择">
            <el-option
              v-for="item in dataTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否默认" :label-width="formLabelWidth" prop="isDefault">
          <el-switch
            v-model="formData.isDefault"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import dataSourceApi from '@/api/generator/dataSource'
  export default {
    filters: {
      gmtCreateFilter (code) {
        const date = new Date(code)
        const y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? ('0' + MM) : MM
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h
        let m = date.getMinutes()
        m = m < 10 ? ('0' + m) : m
        let s = date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
      }
    },
    data () {
      return {
        list: [],
        listLoading: false,
        pageSize: 20,
        total: 0,
        pageNo: 1,
        formData: {
          id: '',
          className:'',
          url: '',
          userName: '',
          password: '',
          dataType: '',
          isDefault: false
        },
        dialogFormVisible: false,
        rules: {
          className: [
            { required: true, message: '请填写驱动', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '请填写连接地址', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '请填写连接用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请填写连接密码', trigger: 'blur' }
          ],
          dataType: [
            { required: true, message: '请选择数据库类型', trigger: 'blur' }
          ]
        },
        formLabelWidth: '120px',
        dataTypes: [
          {label: 'Mysql', value: '0'},
          {label: 'Oracle', value: '1'}
        ]
      }
    },
    created() {
      this.getDataList()
    },
    methods: {
      // 查询列表
      getDataList() {
        this.listLoading = false
        dataSourceApi.listAll({
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }).then(res => {
          this.list = res.data.pages.records
          this.total = res.data.pages.total
          this.listLoading = false
        }).catch(res => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      // 每页多少条数据
      handleSizeChange(val) {
        this.pageSize = val
      },
      // 当前第几页
      handleCurrentChange(val) {
        this.pageNo = val
      },
      // 新增
      addData() {
        this.dialogFormVisible = true
        this.formData = {
          id: '',
          className:'',
          url: '',
          userName: '',
          password: '',
          dataType: '',
          isDefault: false
        }
      },
      // 编辑
      editHandle(id) {
        this.dialogFormVisible = true
        this.formData.id = id
        dataSourceApi.listById(id).then(res => {
          this.formData = res.data.info
          if (res.data.info.dataType === 0) {
            this.formData.dataType = '0'
          } else {
            this.formData.dataType = '1'
          }
          this.formData.updateTime = ''
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      // 删除
      removeHandle(id) {
        this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          dataSourceApi.removeById(id).then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(res.message)
            }
          }).catch()
        }).catch(() => {
          this.$message('取消')
        })
      },
      submitForm() {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            dataSourceApi.saveOrUpdate(this.formData).then(res => {
              if (res.code === 0) {
                this.$message({
                  message: res.message,
                  type: 'success'
                })
                this.dialogFormVisible = false
                this.getDataList()
              }
            }).catch(res => {
              this.$message.error(res.message)
            })
          } else {
            return false
          }
        })
      },
      // 测试连接
      testHandle(id) {
        dataSourceApi.testDataSource(id).then(res => {
          if (res.data.result) {
            this.$message({
              message: '连接成功',
              type: 'success'
            })
          } else {
            this.$message.error('连接失败')
          }
        }).catch(res => {
          this.$message.error(res.message)
        })
      }
    }
  }
</script>

<style scoped></style>